import {config} from '../config';
class http{
  request(params){
    if(!params.method){
      params.method="GET"
    }
    wx.request({
      url: config.base_api_url+params.url,
      data:params.data,
      method:params.method,
      header: {
        'Content-Type': 'json'
      },
      success: function(res) {
        // console.log(res)
        let statusCode=res.statusCode.toString();
        if(statusCode.startsWith("2")){
          params.success(res.data);
        }else{
          wx.showToast({
            title: '网络错误',
            icon: 'none',
          })
        }
      },
      fail:function(){
        wx.showToast({
          title: '错误',
          icon: 'none'
        })
      }
    })
  }
}
function list(subjects){
   console.log(subjects)
  var movies=[];
  subjects.forEach(ele=>{
    var title=ele.title;
    var average=ele.rating.average;
    var year=ele.title+'('+ele.year+')';
    var directors=ele.directors[0].name;
    var imgUrl=ele.images.small;
    var id=ele.id;
    var temp={
    title,
    average,
    year,
    directors,
    imgUrl,
    id,
  }
  movies.push(temp)
  })
  return movies
}
function cast(casts){
  var arr=[];
  casts.forEach(ele=>{
    arr.push(ele.name)
  })
  var newArr=arr.join(" ")
  return newArr
}
export{
  http,
  list,
  cast
}